#ifndef JPINGSOCKET_H
#define JPINGSOCKET_H

#include <QTENetwork/qtenetworkglobal.h>
#include <QObject>

class QHostAddress;

namespace QTE
{

class JICMPSocketPrivate;

class QTENETWORK_EXPORT JICMPSocket : public QObject
{
	Q_OBJECT

public:
	explicit JICMPSocket(QObject *parent = nullptr);
	explicit JICMPSocket(quint64 timeout, QObject *parent = nullptr);
	~JICMPSocket();

public Q_SLOTS:
	virtual int ping(const QString &hostName, int cnt = 3); // domain name or ip
	int ping(const QHostAddress &address, int cnt = 3);

public:
	void setTimeout(quint64 msecs);
	quint64 timeout() const;

public:
	struct PingResponse
	{
		int     taskID      = -1;
		QString hostAddress = "";
		quint64 averageTime = 0; //us
		quint64 totalTime   = 0; //us
		int     successCnt  = 0;
		int     totalCnt    = 0;
	};

Q_SIGNALS:
	void pong(const JICMPSocket::PingResponse &response);
	void error();

public:
	// return  0:success  1:timeout  -1:error
	virtual int waitForPong(PingResponse &response, int timeout = 30000);
	QString errorString() const;

protected:
	void setErrorString(const QString &errstr);

private:
	JICMPSocketPrivate *d_ptr;
};

} //namespace QTE

Q_DECLARE_METATYPE(QTE::JICMPSocket::PingResponse)


#endif //JPINGSOCKET_H
